package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.MobilePermission;
import com.xmy.cultivate.entity.Permission;
import com.xmy.cultivate.entity.StaffRanking;
import com.xmy.cultivate.service.IMobilePermissionService;
import com.xmy.cultivate.service.IMobileRolePermissionService;
import com.xmy.cultivate.service.IMobileStaffPermissionService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 手机端权限权限 前端控制器
 * </p>
 *
 * @author hpiggy
 * @since 2024-06-26
 */
@RestController
@RequestMapping("/api/mobilePermission")
public class MobilePermissionController {
    @Autowired
    IMobilePermissionService iMobilePermissionService;

    @Autowired
    IMobileRolePermissionService iMobileRolePermissionService;


    @ApiOperation("手机端权限列表查看")
    @GetMapping()
    public Result findAll(@RequestParam Map reMap) {
        String type = (String) reMap.get("type");
        QueryWrapper<MobilePermission> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(type), "type", type);
        List<MobilePermission> permissionList = iMobilePermissionService.list(queryWrapper);
        return new Result(Code.OK, new PageResult(permissionList, (long) permissionList.size(), 1L));
    }

    @ApiOperation("通过id查询权限")
    @GetMapping("/{id}")
    public Result findById(@PathVariable(value = "id") String id) {
        MobilePermission permission = iMobilePermissionService.getById(id);
        return new Result(Code.OK, permission);
    }

    @ApiOperation("添加权限")
    @PostMapping("")
    public Result save(@RequestBody MobilePermission permission) {
        boolean rs = iMobilePermissionService.save(permission);
        return new Result(rs ? Code.OK : Code.ERROR);
    }

    @ApiOperation("修改权限")
    @PutMapping("")
    public Result update(@RequestBody MobilePermission permission) {
        boolean rs = iMobilePermissionService.updateById(permission);
        return new Result(rs ? Code.OK : Code.ERROR);
    }

    @ApiOperation("删除权限")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable(value = "id") Long id) {
        boolean rs = iMobilePermissionService.removeById(id);
        return new Result(rs ? Code.OK : Code.ERROR);
    }

}
